package com.daon.controller;

import com.daon.common.ServerResponse;
import com.daon.param.SysUserParam;
import com.daon.pojo.SysUser;
import com.daon.service.ISysRoleService;
import com.daon.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Author 1daon
 * @EMAIL 532418116@qq.com
 * @Version 创建时间：2019/1/18 9:38 PM
 * @Modified By 后台用户管理
 */
@Controller
@RequestMapping("/sys/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleService iSysRoleService;

    @RequestMapping("index.page")
    public ModelAndView index(@RequestParam(value = "searchUser", defaultValue = "") String searchUser,
                              @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        ModelAndView mav = new ModelAndView("/backend/sysUser/index");
        PageInfo sysUserVoList = iSysUserService.getUserListByKeyword(pageNum,pageSize, StringUtil.isEmpty(searchUser)?null:'%'+searchUser+'%');
        mav.getModel().put("searchUser", searchUser);
        mav.getModel().put("sysUserVoList", sysUserVoList);
        return mav;
    }
    @RequestMapping("add.page")
    public ModelAndView add(@RequestParam(value = "deptId", defaultValue = "0") int deptId){
        ModelAndView mav = new ModelAndView("/backend/sysUser/add");

        mav.getModel().put("deptId", deptId);
        return mav;
    }
    @RequestMapping("edit.page")
    public ModelAndView edit(@RequestParam(value = "userId", defaultValue = "0") int userId){
        ModelAndView mav = new ModelAndView("/backend/sysUser/edit");

        return mav;
    }
    @RequestMapping("detail.page")
    public ModelAndView detail(@RequestParam(value = "userId", defaultValue = "0") int userId){
        ModelAndView mav = new ModelAndView("/backend/sysUser/detail");
        return mav;
    }
    @RequestMapping("userBaseInformation.page")
    public ModelAndView userBaseInformation(){
        ModelAndView mav = new ModelAndView("/backend/sysUser/baseInfo");
        return mav;
    }
    @RequestMapping(value="add.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<SysUser> add(SysUserParam sysUserParam){
        ServerResponse<SysUser> resp = iSysUserService.save(sysUserParam);
        return resp;
    }
    @RequestMapping(value="edit.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse edit(SysUserParam sysUserParam){
        ServerResponse resp = iSysUserService.update(sysUserParam);
        return resp;
    }
    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(@RequestParam(value = "userId", defaultValue = "0") int userId){
        ServerResponse resp = iSysUserService.updateDeleteSysUserById(userId);
        return resp;
    }
    @RequestMapping(value = "disable.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse disable(@RequestParam(value = "userId", defaultValue = "0") int userId){
        ServerResponse resp = iSysUserService.updateStatusSysUserById(userId);
        return resp;
    }
    @RequestMapping(value="checkMobile.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse checkMobile(@RequestParam(value = "userMobile", defaultValue = "") String userMobile){
        ServerResponse resp = iSysUserService.checkMobile(userMobile);
        return resp;
    }
}
